package com.dongnao.network.netty_protobuf;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

public class MyClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelRegistered");
        super.channelRegistered(ctx);
    }

    //与服务器通道就绪触发方法
    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        System.out.println("客户端上下文 " + ctx);

        studentPOJO.student.Builder builder = studentPOJO.student.newBuilder();
        builder.setId(123);
        builder.setName("张三");
        studentPOJO.student student = builder.build();

//        ctx.writeAndFlush(Unpooled.copiedBuffer("你好服务器", CharsetUtil.UTF_8));
        ctx.writeAndFlush(student);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
        super.channelInactive(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        System.out.println("exceptionCaught");
        cause.printStackTrace();
        ctx.close();
    }
}
